package com.yzy.fly.aspect;

import com.yzy.fly.function.logging.Log;
import com.yzy.fly.function.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//@Slf4j
@Aspect
@Component
public class URLCallStatisticsAspect {
    private static Log log = LogFactory.getLog(URLCallStatisticsAspect.class);

    // 构造一个线程池
    private ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
            new ThreadPoolExecutor.DiscardOldestPolicy());

    private final static String TOPIC_NAME = "URL"; //topic的名称

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Pointcut("@annotation(requestMapping)")
    public void excudeService(RequestMapping requestMapping) {
    }

    @Around("excudeService(requestMapping)")
    public Object doAround(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
        // 获得request对象
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        String requestURI = request.getRequestURI();
//        System.out.println(requestURI);
        threadPool.execute(() -> {
            String[] value = requestMapping.value();
//            ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(TOPIC_NAME, "key", requestURI);
//            future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
//                @Override
//                public void onFailure(Throwable ex) {
//                    ex.printStackTrace();
//                }
//
//                @Override
//                public void onSuccess(SendResult<String, String> result) {
//                    System.out.println(result.getProducerRecord());
//                    System.out.println(result.getRecordMetadata());
//                }
//            });
        });
        // result的值就是被拦截方法的返回值
        Object result = pjp.proceed();


        return result;
    }

//    @After("excudeService(requestMapping)")
//    public void doAfter(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
//        // 获得request对象
//        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//        HttpServletRequest request = sra.getRequest();
//        String requestURI = request.getRequestURI();
//        System.out.println(requestURI);
//
//        ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(TOPIC_NAME, "key", requestURI);
//        future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
//            @Override
//            public void onFailure(Throwable ex) {
//                ex.printStackTrace();
//            }
//            @Override
//            public void onSuccess(SendResult<String, String> result) {
//                System.out.println(result.getProducerRecord());
//                System.out.println(result.getRecordMetadata());
//            }
//        });
//    }

}
